
import {  AttrChangeEnum, DmgType, UnitAttr, UnitAttrStruct } from "../types/L_Type";
import L_AttrControlUtil from "../utils/L_AttrControlUtil";
import {FieldEnum, FieldEnumField} from "../constant/FieldEnum";
import { DMG_TYPE_CONFIG } from "../config/DmgFieldConfig";
import EnhItemAttr from "./EnhItemAttr";
import EnvConfig from "../config/EnvConfig";

/** 物品增强类 绑定物品所有额外属性 */
export default class ItemX{
 
    item:item;  //对象句柄 CreateUnit方法的返回值

    handle:number;  //指针地址 GetHandleId(u) 的值
  
    name:string;
    
    lvl?: number;
  
    private _pSkill?: string[];
  
    private _aSkill?: string[];
  
    private _attrs?: UnitAttr;
  
    private _dtRes?: DmgType;
  
    private _dtEnh?: DmgType;

    // enhId?:number;  //是强化物品 则拥有唯一强化id

    // enhNum:number=0  //强化次数

    private _enhInfo?: EnhItemAttr;
  


    constructor(item:any,initFields?:UnitAttrStruct,handle?:number,name?:string){
        this.item=item;
        this.handle=handle || GetHandleId(item);
        this.name=name || "测试用物品"//GetUnitName(unit);    
        //物品记录属性就可以了      
        this._attrs=initFields?.attrs;
        this._dtRes=initFields?.dtRes;
        this._dtEnh=initFields?.dtEnh;   

        //console.log("--->itemx初始化属性",L_String.getObjJSONStr(initFields))
        //console.log("--->itemx初始化属性完成")
      }
  
   /**
    * 增加单位的属性
    * @param incFields  增加的属性对象
    */
    public  incrItemAttrs(incFields:UnitAttrStruct){
      this.changeUnitAttrs(incFields,AttrChangeEnum.INCREASE)
    }
  
     /**
    * 减少单位的属性
    * @param incFields  减少的属性对象
    */
    public  redUnitAttrs(redFields:UnitAttrStruct){
      this.changeUnitAttrs(redFields,AttrChangeEnum.REDUCE)
    }
  
  
    /**
     * 改变单位属性
     * @param incFields 改变属性对象
     * @param incType 增加|减少枚举
     * @returns 
     */
    private  changeUnitAttrs(incFields:UnitAttrStruct,incType:AttrChangeEnum){
      if(incFields==null) return
      //1.第一类 二进制属性+自定义基础属性
      if(incFields.attrs!=undefined){
          //先直接修改ux对象  合并属性
          L_AttrControlUtil.mergeField(this.attrs,incFields.attrs,incType);
      }
      //2.第二类元素增强
      if(incFields.dtEnh!=undefined){
          L_AttrControlUtil.mergeField(this.dtEnh,incFields.dtEnh,incType)
      }
      //3.第三类元素抗性
      if(incFields.dtRes!=undefined){
          L_AttrControlUtil.mergeField(this.dtRes,incFields.dtRes,incType)
      }
  
  }  

   public getItemNameWithEnh():string{
    if(this.enhInfo!=undefined){
      return "+"+this.enhInfo.enhNum+" "+this.name
    }else{
      return this.name
    }
  }

   public getItemxDesc():string{
  

    let rs=""
    let nRowSymbol="\n "
    if(this._attrs!=undefined){
      let attrGroupMap:Map<number,string>=new Map()
      //根据单位类型从配置表获取初始化的配置   I01O->1227895119
      let configAttr:any=L_AttrControlUtil.getItemConfigAttr(this._enhInfo?.originItemTypeStr || 'undefined')
      for(let k in this._attrs){
        let fef:FieldEnumField=(FieldEnum.ATTR_FIELD as any)[k] 
        let showGroupV=fef.showGroup ?? 99
        let groupStr:string | undefined=attrGroupMap.get(showGroupV) ?? undefined
        let curStr:string
        let v=(this._attrs as any)[k]

        //是其中的某一项就将比对值加上
        if(fef.showCompareWithCfg ){
          curStr=fef.name+":"+v
          let cv=v-(configAttr.attrs?.[k] || 0)
          if(cv!=0) curStr+="(+"+cv+")"
        }else{
          curStr=fef.name+":"+v
        }
        //分组已有值和无值情况
        if(groupStr==undefined){
          groupStr=curStr
        }else{
          groupStr+=","+curStr
        }
        //保存
        attrGroupMap.set(showGroupV,groupStr)
      }
      //字符串分组 通过排序索引重新排序 按顺序输出
      if(attrGroupMap.size>0){
        let arrayAttrs=Array.from(attrGroupMap);
        
        // if(EnvConfig.devMod){
        //   arrayAttrs.sort(function(a:any,b:any){return a[0]-b[0]})
        // }else{
        //   arrayAttrs.sort(function(a:any,b:any){return a[1]-b[1]})
        // }
        //ts 和lua索引是0和1  靠环境变量还是不好区分 先用异常捕获写法
        try{
          arrayAttrs.sort(function(a:any,b:any){return a[0]-b[0]})
        }catch(e){
          arrayAttrs.sort(function(a:any,b:any){return a[1]-b[1]})
        }
        for(let i=0;i<arrayAttrs.length;i++){
          if(i!=0) rs+=nRowSymbol
          rs+=arrayAttrs[i][1]
        }
      }

      // let enhStr=""
      // let rowEnd=""
      // for(let k in this._attrs){
      //   let v=(this._attrs as any)[k]
      //   let rowStart=","
      //   if(rowEnd!="") rowStart=""
      //    //攻击防御直接换行 新的一行
      //   if(k==FieldEnum.ATTR_FIELD.atk.key || k==FieldEnum.ATTR_FIELD.def.key){
      //     rowStart=""
      //     rowEnd=nRowSymbol
      //   }else{
      //     rowEnd=""
      //   } 

      //   //是其中的某一项就将比对值加上
      //   if(k==FieldEnum.ATTR_FIELD.atk.key || k==FieldEnum.ATTR_FIELD.def.key || k==FieldEnum.ATTR_FIELD.hp.key || k==FieldEnum.ATTR_FIELD.mp.key ){
      //     if(rowStart==",")enhStr+=rowStart
      //     enhStr+=(FieldEnum.ATTR_FIELD as any)[k].name+":"+v
      //     let cv=v-(configAttr?.attrs?.[k] || 0)
      //     if(cv!=0) enhStr+="(+"+cv+")"
      //   }else{
      //     enhStr+=","+(FieldEnum.ATTR_FIELD as any)[k].name+":"+v
      //   }
      //   enhStr+=rowEnd
      //   rowIndex++
      // }
      // if(enhStr.startsWith(",")) enhStr=enhStr.substring(1)
      // rs+=enhStr
    }
    if(this._dtEnh!=undefined){
      let enhStr=""
      for(let k in this._dtEnh){
        enhStr=enhStr+","+(DMG_TYPE_CONFIG as any)[k].name+"增效:+"+(this._dtEnh as any)[k]+"%"
      }
      enhStr=enhStr.substring(1)
      rs=rs+nRowSymbol+enhStr
    }
    if(this._dtRes!=undefined){
      let enhStr=""
      for(let k in this._dtRes){
        enhStr=enhStr+","+(DMG_TYPE_CONFIG as any)[k].name+"抗性:+"+(this._dtRes as any)[k]+"%"
      }
      enhStr=enhStr.substring(1)
      rs=rs+nRowSymbol+enhStr
    } 

    return rs
   }
    
    
      
  
      public get pSkill(): string[] | undefined {
        return this._pSkill;
      }
      public set pSkill(value: string[] | undefined) {
        this._pSkill = value;
      }
  
      public get aSkill(): string[] | undefined {
        return this._aSkill;
      }
      public set aSkill(value: string[] | undefined) {
        this._aSkill = value;
      }
  
      public get attrs(): UnitAttr | undefined {
        if(this._attrs==undefined) this._attrs={}
        return this._attrs;
      }
      public set attrs(value: UnitAttr | undefined) {
        this._attrs = value;
      }
  
      public get dtRes(): DmgType | undefined {
        if(this._dtRes==undefined) this._dtRes={}
        return this._dtRes;
      }
      public set dtRes(value: DmgType | undefined) {
        this._dtRes = value;
      }
  
      public get dtEnh(): DmgType | undefined {
        if(this._dtEnh==undefined) this._dtEnh={}
        return this._dtEnh;
      }
      public set dtEnh(value: DmgType | undefined) {
        this._dtEnh = value;
      }
  

    public get enhInfo(): EnhItemAttr | undefined  {
      return this._enhInfo;
    }
    public set enhInfo(value: EnhItemAttr) {
      this._enhInfo = value;
    }
}  